Imports System.Diagnostics.Process
Imports System.Threading
Public Class F_SI_JobOrder
    Dim WithEvents SBO_Application As SAPbouiCOM.Application
    Dim Ocompany As SAPbobsCOM.Company
    Sub New(ByVal ocompany1 As SAPbobsCOM.Company, ByVal sbo_application1 As SAPbouiCOM.Application)
        SBO_Application = sbo_application1
        Ocompany = ocompany1
    End Sub
    Public Sub SI_Job_Bind(ByVal oForm As SAPbouiCOM.Form)
        Try
            oForm.PaneLevel = 1
            DocNumber_SI()
            ShippigNameLoad()
            oItem = oForm.Items.Item("153")
            oItem.Enabled = False
            oEdit = oForm.Items.Item("SIJ18").Specific
            oEdit.String = Format(Now.Date, "dd/MM/yy")
            ooption = oForm.Items.Item("28").Specific
            ooption.GroupWith("SIJ1000001")
            ooption = oForm.Items.Item("85").Specific
            ooption.GroupWith("86")
            oCombo = oForm.Items.Item("36").Specific
            ComboLoad_City(oForm, oCombo)
            oCombo = oForm.Items.Item("38").Specific
            ComboLoad_City(oForm, oCombo)
            oCombo = oForm.Items.Item("42").Specific
            ComboLoad_Whsc(oForm, oCombo)
            oCombo = oForm.Items.Item("30").Specific
            ComboLoad_ContainerType(oForm, oCombo)
            oCombo = oForm.Items.Item("1000002").Specific
            ComboLoad_Currency(oForm, oCombo)
            oCombo = oForm.Items.Item("1000003").Specific
            oCombo.ValidValues.Add("Cheque", "C")
            oCombo.ValidValues.Add("Cash", "Ch")
            oCombo.ValidValues.Add("CC", "CC")
            oCombo.ValidValues.Add("Online", "O")

            '-----DO
            oMatrix = oForm.Items.Item("SIJDOMAT").Specific
            oColumns = oMatrix.Columns
            oMatrix.AddRow()
            '------------VO
            oMatrix1 = oForm.Items.Item("148").Specific
            oColumns = oMatrix1.Columns
            oMatrix1.AddRow()

            '---- goods Receipt

            oForm.DataSources.UserDataSources.Add("V_0", SAPbouiCOM.BoDataType.dt_SHORT_TEXT)
            oMatrix3 = oForm.Items.Item("SIJGR").Specific
            oColumns = oMatrix3.Columns
            oColumn = oColumns.Item("V_0")
            oColumn.DataBind.SetBound(True, "", "V_0")
            oItem = oForm.Items.Item("SIJGR")
            oItem.Width = 150
            oItem.Height = 30
            oColumn.Width = 130
            oMatrix3.AddRow()

            'CFL
            CFL_Item_Vessel(oForm, SBO_Application)
            oEdit = oForm.Items.Item("SIJ27").Specific
            oEdit.ChooseFromListUID = "OITM11"
            oEdit.ChooseFromListAlias = "ItemCode"
            
            '  ComboLoad_PaymentType(oForm, oCombo)
            oForm.DataBrowser.BrowseBy = "SIJ16"
        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
        End Try
    End Sub
#Region "ComboLoad"
    Private Sub ComboLoad_City(ByRef Oform As SAPbouiCOM.Form, ByRef oCombo As SAPbouiCOM.ComboBox)
        Try
            oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT T0.[Code], T0.[Name] FROM OCRY T0 order by T0.COde")
            Dim it As Integer = 1
            For it = oCombo.ValidValues.Count - 1 To 0 Step -1
                oCombo.ValidValues.Remove(it, SAPbouiCOM.BoSearchKey.psk_Index)
            Next
            Do While Not oRecordSet.EoF
                oCombo.ValidValues.Add(oRecordSet.Fields.Item(0).Value, oRecordSet.Fields.Item(1).Value)
                oRecordSet.MoveNext()
            Loop
            oRecordSet = Nothing
            GC.Collect()
        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
        End Try
    End Sub
    Private Sub ComboLoad_Currency(ByRef Oform As SAPbouiCOM.Form, ByRef oCombo As SAPbouiCOM.ComboBox)
        Try
            oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT T0.[CurrCode], T0.[CurrName] FROM OCRN T0 ORDER BY T0.[CurrCode]")
            Dim it As Integer = 1
            For it = oCombo.ValidValues.Count - 1 To 0 Step -1
                oCombo.ValidValues.Remove(it, SAPbouiCOM.BoSearchKey.psk_Index)
            Next
            Do While Not oRecordSet.EoF
                oCombo.ValidValues.Add(oRecordSet.Fields.Item(0).Value, oRecordSet.Fields.Item(1).Value)
                oRecordSet.MoveNext()
            Loop
            oRecordSet = Nothing
            GC.Collect()
        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
        End Try
    End Sub
    Private Sub ComboLoad_ContainerType(ByRef Oform As SAPbouiCOM.Form, ByRef oCombo As SAPbouiCOM.ComboBox)
        Try
            oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT T0.[Code], T0.[Name] FROM [@AB_SEAI_CONTTYPE] T0 order by T0.COde")
            Dim it As Integer = 1
            For it = oCombo.ValidValues.Count - 1 To 0 Step -1
                oCombo.ValidValues.Remove(it, SAPbouiCOM.BoSearchKey.psk_Index)
            Next
            Do While Not oRecordSet.EoF
                oCombo.ValidValues.Add(oRecordSet.Fields.Item(0).Value, oRecordSet.Fields.Item(1).Value)
                oRecordSet.MoveNext()
            Loop
            oRecordSet = Nothing
            GC.Collect()
        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
        End Try
    End Sub
    Private Sub ComboLoad_Whsc(ByRef Oform As SAPbouiCOM.Form, ByRef oCombo As SAPbouiCOM.ComboBox)
        Try
            oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT T0.[WhsCode], T0.[WhsName] FROM OWHS T0 ORDER BY T0.[WhsCode]")
            Dim it As Integer = 1
            For it = oCombo.ValidValues.Count - 1 To 0 Step -1
                oCombo.ValidValues.Remove(it, SAPbouiCOM.BoSearchKey.psk_Index)
            Next
            Do While Not oRecordSet.EoF
                oCombo.ValidValues.Add(oRecordSet.Fields.Item(0).Value, oRecordSet.Fields.Item(1).Value)
                oRecordSet.MoveNext()
            Loop
            oRecordSet = Nothing
            GC.Collect()
        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
        End Try
    End Sub
    Private Sub ComboLoad_PaymentType(ByRef Oform As SAPbouiCOM.Form, ByRef oCombo As SAPbouiCOM.ComboBox)
        Try
            oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT T0.[WhsCode], T0.[WhsName] FROM OWHS T0 ORDER BY T0.[WhsCode]")
            Dim it As Integer = 1
            For it = oCombo.ValidValues.Count - 1 To 0 Step -1
                oCombo.ValidValues.Remove(it, SAPbouiCOM.BoSearchKey.psk_Index)
            Next
            Do While Not oRecordSet.EoF
                oCombo.ValidValues.Add(oRecordSet.Fields.Item(0).Value, oRecordSet.Fields.Item(1).Value)
                oRecordSet.MoveNext()
            Loop
            oRecordSet = Nothing
            GC.Collect()
        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
        End Try
    End Sub
    Public Sub DocNumber_SI()
        Try
            Dim fdt As String = ""
            Dim tdt As String = ""
            fdt = Format(Now.Date, "yyyy-MM-dd")
            fdt = fdt.Substring(0, 8) & "01"
            oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1.DoQuery("SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,getdate()))),DATEADD(mm,1,getdate())),101)")
            tdt = oRecordSet1.Fields.Item(0).Value
            oRecordSet2 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet2.DoQuery("SELECT (count(*)+1) as CountNo FROM [dbo].[@AB_SEAI_JOB_H]  T0 WHERE T0.[CreateDate]  between '" & fdt & "' and '" & tdt & "'")
            Dim DocNum As Integer = oRecordSet2.Fields.Item(0).Value
            oEdit = oForm.Items.Item("SIJ16").Specific
            Dim DocNumLen As Integer
            DocNumLen = DocNum.ToString.Length
            If DocNum = 0 Then
                oEdit.String = "GKSI" & Format(Now.Date, "yyyyMMdd") & "0001"
            ElseIf DocNumLen = 1 And DocNum <> 0 Then
                oEdit.String = "GKSI" & Format(Now.Date, "yyyyMMdd") & "000" & DocNum
            ElseIf DocNumLen = 2 Then
                oEdit.String = "GKSI" & Format(Now.Date, "yyyyMMdd") & "00" & DocNum
            ElseIf DocNumLen = 3 Then
                oEdit.String = "GKSI" & Format(Now.Date, "yyyyMMdd") & "0" & DocNum
            ElseIf DocNumLen = 4 Then
                oEdit.String = "GKSI" & Format(Now.Date, "yyyyMMdd") & "" & DocNum
            ElseIf DocNumLen = 5 Then
                oEdit.String = "GKSI" & Format(Now.Date, "yyyyMMdd") & DocNum
            End If
            '--------DO NO
            oEdit = oForm.Items.Item("115").Specific
            DocNumLen = DocNum.ToString.Length
            If DocNum = 0 Then
                oEdit.String = "GKWI" & Format(Now.Date, "yyyyMMdd") & "0001"
            ElseIf DocNumLen = 1 And DocNum <> 0 Then
                oEdit.String = "GKWI" & Format(Now.Date, "yyyyMMdd") & "000" & DocNum
            ElseIf DocNumLen = 2 Then
                oEdit.String = "GKWI" & Format(Now.Date, "yyyyMMdd") & "00" & DocNum
            ElseIf DocNumLen = 3 Then
                oEdit.String = "GKWI" & Format(Now.Date, "yyyyMMdd") & "0" & DocNum
            ElseIf DocNumLen = 4 Then
                oEdit.String = "GKWI" & Format(Now.Date, "yyyyMMdd") & "" & DocNum
            ElseIf DocNumLen = 5 Then
                oEdit.String = "GKWI" & Format(Now.Date, "yyyyMMdd") & DocNum
            End If


        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
        End Try

    End Sub
    Public Sub ShippigNameLoad()
        Try

            oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1.DoQuery("SELECT UPPER(T0.[CompnyName]), UPPER(T0.[CompnyAddr]) FROM OADM T0")
            oEdit = oForm.Items.Item("94").Specific
            oEdit.String = oRecordSet1.Fields.Item(0).Value.ToString
            oEdit = oForm.Items.Item("101").Specific
            oEdit.String = oRecordSet1.Fields.Item(1).Value.ToString


        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
        End Try

    End Sub
#End Region
    Public Sub CreatePO()
        Try
            Dim oform1 As SAPbouiCOM.Form
            oform1 = SBO_Application.Forms.GetFormByTypeAndCount(133, 1)
            oMatrix2 = oform1.Items.Item("38").Specific
            oColumns = oMatrix2.Columns
            Dim i As Integer
            Dim k As Integer
            Dim oPO As SAPbobsCOM.Documents
            Dim VenCode As String = ""
            Dim ChrgCode As String = ""
            For i = 1 To oMatrix2.RowCount
                oEdit = oMatrix2.Columns.Item("U_AB_Vendor").Cells.Item(i).Specific
                VenCode = oEdit.String
                oEdit = oMatrix2.Columns.Item("1").Cells.Item(i).Specific
                ChrgCode = oEdit.String
                If ChrgCode <> "" And VenCode <> "" Then
                    oPO = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oPurchaseOrders)
                    oPO.TaxDate = Now.Date
                    oPO.DocDate = Now.Date
                    oPO.CardCode = VenCode
                    oPO.Lines.ItemCode = ChrgCode
                    oPO.Lines.Add()
                    k = oPO.Add()
                    Dim st As String = ""
                    Ocompany.GetLastError(k, st)
                    If k = 0 Then
                        SBO_Application.StatusBar.SetText("Line No:" & i & " PO Created Success", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Success)
                    Else
                        SBO_Application.StatusBar.SetText("Line No:" & i & " PO Created Failed.Error Message:" & st & "", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                    End If
                End If
            Next
        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
        End Try
    End Sub
    Public Sub LoadDraftInvoice(ByVal JobNo As String)
        Try
            oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1.DoQuery("SELECT T0.[U_CCode], T0.[U_CName],  T0.[U_JobNo], T0.[U_VessName], T0.[U_OBL], T0.[U_HBL], T0.[U_TotPkg], T0.[U_GrssWt], T0.[U_ETD],T0.[U_LoadPortC], T0.[U_DisPortC],T0.[U_QNo] FROM [dbo].[@AB_SEAI_JOB_H] T0 WHERE T0.[U_JobNo] ='" & JobNo & "'")
            If oRecordSet1.RecordCount = 0 Then
                SBO_Application.StatusBar.SetText("No Data Found!", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                Exit Sub
            End If
            Dim oform1 As SAPbouiCOM.Form
            SBO_Application.ActivateMenuItem("2053")
            oform1 = SBO_Application.Forms.GetFormByTypeAndCount(133, 1)
            oform1.Title = "A/R Invoice - Draft"
            oItem = oform1.Items.Item("1")
            oItem.Visible = False
            Dim oNewItem As SAPbouiCOM.Item
            oNewItem = oform1.Items.Add("ADD", SAPbouiCOM.BoFormItemTypes.it_BUTTON)
            oItem = oform1.Items.Item("1")
            oNewItem.Top = oItem.Top
            oNewItem.Height = oItem.Height
            oNewItem.Width = oItem.Width '+ 10
            oNewItem.Left = oItem.Left
            oButton = oNewItem.Specific
            oButton.Caption = "Add Draft"
            oEdit = oform1.Items.Item("4").Specific
            oEdit.String = oRecordSet1.Fields.Item("U_CCode").Value
            oEdit = oform1.Items.Item("54").Specific
            oEdit.String = oRecordSet1.Fields.Item("U_CName").Value
            oform1 = SBO_Application.Forms.GetFormByTypeAndCount(-133, 1)
            oCombo = oform1.Items.Item("U_AB_Divsion").Specific
            oCombo.Select("SI", SAPbouiCOM.BoSearchKey.psk_ByValue)
            oEdit = oform1.Items.Item("U_AB_Origin").Specific
            oEdit.String = oRecordSet1.Fields.Item(2).Value
            oEdit = oform1.Items.Item("U_AB_Destination").Specific
            oEdit.String = oRecordSet1.Fields.Item(3).Value
            oEdit = oform1.Items.Item("U_AB_POA").Specific
            oEdit.String = oRecordSet1.Fields.Item("U_LoadPortC").Value
            oEdit = oform1.Items.Item("U_AB_POD").Specific
            oEdit.String = oRecordSet1.Fields.Item("U_DisPortC").Value
            oEdit = oform1.Items.Item("U_AB_JobNo").Specific
            oEdit.String = oRecordSet1.Fields.Item("U_JobNo").Value
            oEdit = oform1.Items.Item("U_AB_SSIT").Specific
            oEdit.String = oRecordSet1.Fields.Item("U_VessName").Value
            oEdit = oform1.Items.Item("U_AB_MAWB").Specific
            oEdit.String = oRecordSet1.Fields.Item("U_OBL").Value
            oEdit = oform1.Items.Item("U_AB_HAWB").Specific
            oEdit.String = oRecordSet1.Fields.Item("U_HBL").Value
            oEdit = oform1.Items.Item("U_AB_PKGS").Specific
            oEdit.String = oRecordSet1.Fields.Item("U_TotPkg").Value
            oEdit = oform1.Items.Item("U_AB_WT").Specific
            oEdit.String = oRecordSet1.Fields.Item("U_GrssWt").Value
            oEdit = oform1.Items.Item("U_AB_FLT").Specific
            oEdit.String = Format(oRecordSet1.Fields.Item("U_ETD").Value, "dd/MM/yy")
            Dim QTNo As String = oRecordSet1.Fields.Item("U_QNo").Value
            oRecordSet = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet.DoQuery("SELECT T1.[ItemCode], T1.[Dscription], T1.[Quantity], T1.[Price], T0.[DocCur],T1.[U_AB_Vendor] FROM ORDR T0  INNER JOIN RDR1 T1 ON T0.DocEntry = T1.DocEntry WHERE T0.[DocNum] ='" & QTNo & "'")
            oform1 = SBO_Application.Forms.GetFormByTypeAndCount(133, 1)
            oCombo = oform1.Items.Item("63").Specific
            oCombo.Select(oRecordSet.Fields.Item("DocCur").Value, SAPbouiCOM.BoSearchKey.psk_ByValue)
            oMatrix2 = oform1.Items.Item("38").Specific
            oColumns = oMatrix2.Columns
            Dim i As Integer = 0
            For i = 1 To oRecordSet.RecordCount
                oMatrix2.AddRow()
                oEdit = oMatrix2.Columns.Item("1").Cells.Item(i).Specific
                oEdit.String = oRecordSet.Fields.Item("ItemCode").Value
                oEdit = oMatrix2.Columns.Item("3").Cells.Item(i).Specific
                oEdit.String = oRecordSet.Fields.Item("Dscription").Value
                oEdit = oMatrix2.Columns.Item("11").Cells.Item(i).Specific
                oEdit.String = oRecordSet.Fields.Item("Quantity").Value
                oEdit = oMatrix2.Columns.Item("14").Cells.Item(i).Specific
                oEdit.String = oRecordSet.Fields.Item("Price").Value
                oEdit = oMatrix2.Columns.Item("U_AB_Vendor").Cells.Item(i).Specific
                oEdit.String = oRecordSet.Fields.Item("U_AB_Vendor").Value
                oRecordSet.MoveNext()
            Next

            oform1.Title = "A/R Invoice - Draft"
            'SELECT T0.[U_CCode], T0.[U_CName], T0.[U_LoadPortN], T0.[U_DisPortN], T0.[U_JobNo], T0.[U_VessName], T0.[U_OBL], T0.[U_HBL], T0.[U_TotPkg], T0.[U_GrssWt], T0.[U_ETD] FROM [dbo].[@AB_SEAI_JOB_H]  T0 WHERE T0.[U_JobNo] =''")
        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
        End Try
    End Sub
    Public Sub LoadJobOrder(ByVal SQNO As String)
        Try
            'SELECT T0.[CardCode], T0.[CardName], T0.[U_AB_Divison], T0.[U_AB_TransTo], T0.[U_AB_Trnst], T0.[U_AB_VessCode], T0.[U_AB_VessName], T0.[U_AB_OrginCode], T0.[U_AB_OriginName], T0.[U_AB_DestCode], T0.[U_AB_DestName], T0.[U_AB_Desc], T0.[U_AB_Validity], T0.[U_AB_Ttime], T0.[U_AB_Freq], T0.[U_AB_CARTotQt], T0.[U_AB_CARTotWt] FROM ORDR T0 WHERE T0.[DocNum] ='1'
            oRecordSet1 = Ocompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset)
            oRecordSet1.DoQuery("SELECT T0.DocNum,T0.[CardCode], T0.[CardName], T1.[Name], T0.[U_AB_VessCode], T0.[U_AB_VessName], T0.[U_AB_OrginCode], T0.[U_AB_OriginName], T0.[U_AB_DestCode], T0.[U_AB_DestName], T0.[U_AB_Desc],T0.[Address],T0.U_AB_CaType FROM ORDR T0  Left JOIN OCPR T1 ON T0.CntctCode = T1.CntctCode WHERE T0.[DocNum] ='" & SQNO & "' and isnull(T0.U_AB_JobNo,'')='' and T0.[U_AB_Divison]='SI'")
            If oRecordSet1.RecordCount = 0 Then
                SBO_Application.StatusBar.SetText("No Data Found!", SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                Exit Sub
            End If
            oEdit = oForm.Items.Item("SIJ4").Specific
            oEdit.String = oRecordSet1.Fields.Item(0).Value
            oEdit = oForm.Items.Item("SIJ6").Specific
            oEdit.String = oRecordSet1.Fields.Item(1).Value
            oEdit = oForm.Items.Item("SIJ8").Specific
            oEdit.String = oRecordSet1.Fields.Item(2).Value
            oEdit = oForm.Items.Item("SJI10").Specific
            oEdit.String = oRecordSet1.Fields.Item(3).Value
            oEdit = oForm.Items.Item("SIJ27").Specific
            oEdit.String = oRecordSet1.Fields.Item(4).Value
            oEdit = oForm.Items.Item("24").Specific
            oEdit.String = oRecordSet1.Fields.Item(5).Value
            Try
                oCombo = oForm.Items.Item("36").Specific
                oCombo.Select(oRecordSet1.Fields.Item(6).Value, SAPbouiCOM.BoSearchKey.psk_ByValue)
            Catch ex As Exception

            End Try
            Try
                oCombo = oForm.Items.Item("38").Specific
                oCombo.Select(oRecordSet1.Fields.Item(8).Value, SAPbouiCOM.BoSearchKey.psk_ByValue)
            Catch ex As Exception

            End Try

            oEdit = oForm.Items.Item("39").Specific
            oEdit.String = oRecordSet1.Fields.Item(7).Value

            oEdit = oForm.Items.Item("40").Specific
            oEdit.String = oRecordSet1.Fields.Item(9).Value
            'T0.[Address]
            oEdit = oForm.Items.Item("96").Specific
            oEdit.String = oRecordSet1.Fields.Item(2).Value & " " & oRecordSet1.Fields.Item("Address").Value

            If oRecordSet1.Fields.Item("U_AB_CaType").Value = "1" Then
                ooption = oForm.Items.Item("28").Specific
                ooption.Selected = True
            End If

            If oRecordSet1.Fields.Item("U_AB_CaType").Value = "2" Then
                ooption = oForm.Items.Item("SIJ1000001").Specific
                ooption.Selected = True
            End If

        Catch ex As Exception
            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
        End Try
    End Sub
    Private Sub SBO_Application_ItemEvent(ByVal FormUID As String, ByRef pVal As SAPbouiCOM.ItemEvent, ByRef BubbleEvent As Boolean) Handles SBO_Application.ItemEvent
        Try
            '-----------Inovice Draft------------
            If pVal.FormType = 133 Then
                If pVal.Before_Action = False And pVal.InnerEvent = False And pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED Then
                    If pVal.ItemUID = "ADD" Then
                        Try
                            If SBO_Application.MessageBox("Would you Like to Create PO?", 1, "Yes", "No") = 1 Then
                                Try
                                    CreatePO()
                                Catch ex As Exception
                                    SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
                                End Try
                            End If
                            SBO_Application.ActivateMenuItem("5907")
                        Catch ex As Exception
                            SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
                        End Try
                    End If
                End If
            End If
            If pVal.FormUID = "SEAI_JOB" Then
                oForm = SBO_Application.Forms.Item("SEAI_JOB")
                '---------------Item Event-----------------------
                If pVal.EventType = SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED Then
                    If pVal.Before_Action = False And pVal.InnerEvent = False Then
                        If pVal.ItemUID = "89" Then
                            oForm.PaneLevel = 1
                        ElseIf pVal.ItemUID = "SO90" Then
                            oForm.PaneLevel = 2
                        ElseIf pVal.ItemUID = "DO1000001" Then
                            oForm.PaneLevel = 3
                        ElseIf pVal.ItemUID = "SIJ125VOU" Then
                            oForm.PaneLevel = 4
                        ElseIf pVal.ItemUID = "153" Then
                            oEdit = oForm.Items.Item("SIJ16").Specific
                            LoadDraftInvoice(oEdit.String)
                        ElseIf pVal.ItemUID = "1" And pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE Then
                            DocNumber_SI()
                            oEdit = oForm.Items.Item("SIJ18").Specific
                            oEdit.String = Format(Now.Date, "dd/MM/yy")
                            ShippigNameLoad()
                            oMatrix3 = oForm.Items.Item("SIJGR").Specific
                            oMatrix3.AddRow()
                            oMatrix = oForm.Items.Item("SIJDOMAT").Specific
                            oMatrix.AddRow()
                            oMatrix1 = oForm.Items.Item("148").Specific
                            oMatrix1.AddRow()
                        End If
                    ElseIf pVal.Before_Action = True And pVal.InnerEvent = False And pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE Then
                        If pVal.ItemUID = "1" Then
                            DocNumber_SI()
                        End If

                    End If
                End If
                '---------------Combo Select-----------------------
                If pVal.EventType = SAPbouiCOM.BoEventTypes.et_COMBO_SELECT Then
                    If pVal.Before_Action = False And pVal.InnerEvent = False And (pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE Or pVal.FormMode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE) Then
                        If pVal.ItemUID = "36" Then
                            oCombo = oForm.Items.Item("36").Specific
                            oEdit = oForm.Items.Item("39").Specific
                            oEdit.String = oCombo.Selected.Description
                        ElseIf pVal.ItemUID = "38" Then
                            oCombo = oForm.Items.Item("38").Specific
                            oEdit = oForm.Items.Item("40").Specific
                            oEdit.String = oCombo.Selected.Description
                        ElseIf pVal.ItemUID = "42" Then
                            oCombo = oForm.Items.Item("42").Specific
                            oEdit = oForm.Items.Item("43").Specific
                            oEdit.String = oCombo.Selected.Description
                        End If
                    End If

                End If
                '---------------Validate Event-----------------------
                If pVal.EventType = SAPbouiCOM.BoEventTypes.et_VALIDATE Then
                    If pVal.Before_Action = False And pVal.InnerEvent = False And pVal.FormMode = SAPbouiCOM.BoFormMode.fm_ADD_MODE Then
                        If pVal.ItemUID = "SIJ4" Then
                            oEdit = oForm.Items.Item("SIJ4").Specific
                            If oEdit.String <> "" Then
                                LoadJobOrder(oEdit.String)
                            End If
                        End If
                        If pVal.ItemUID = "SIJDOMAT" And pVal.ColUID = "V_14" Then
                            oMatrix = oForm.Items.Item("SIJDOMAT").Specific
                            oEdit = oMatrix.Columns.Item("V_14").Cells.Item(oMatrix.RowCount).Specific
                            If oEdit.String <> "" Then
                                oMatrix.AddRow()
                                oMatrix.ClearRowData(oMatrix.RowCount)
                            End If
                        End If
                        If pVal.ItemUID = "148" And pVal.ColUID = "V_8" Then
                            oMatrix1 = oForm.Items.Item("148").Specific
                            oEdit = oMatrix1.Columns.Item("V_8").Cells.Item(oMatrix1.RowCount).Specific
                            If oEdit.String <> "" Then
                                oMatrix1.AddRow()
                                oMatrix1.ClearRowData(oMatrix1.RowCount)
                            End If
                        End If
                    End If
                End If


                ''-----DO
                'oMatrix = oForm.Items.Item("SIJDOMAT").Specific
                'oColumns = oMatrix.Columns
                'oMatrix.AddRow()
                ''------------VO
                'oMatrix1 = oForm.Items.Item("148").Specific
                'oColumns = oMatrix1.Columns
                'oMatrix1.AddRow()

                '--------------Form Resize-------------
                If pVal.EventType = SAPbouiCOM.BoEventTypes.et_FORM_RESIZE Then
                    Try
                        oForm = SBO_Application.Forms.Item("SEAI_JOB")
                        oForm.Freeze(True)
                        oMatrix3 = oForm.Items.Item("SIJGR").Specific
                        oColumns = oMatrix3.Columns
                        oColumn = oColumns.Item("V_0")
                        oItem = oForm.Items.Item("SIJGR")
                        oItem.Width = 150
                        oItem.Height = 30
                        oColumn.Width = 130
                        oForm.Freeze(False)
                    Catch ex As Exception
                    End Try
                    Try
                        If pVal.BeforeAction = False Then
                            oForm.Items.Item("154").Width = oForm.Width - 50
                        End If
                    Catch ex As Exception
                    End Try
                End If
                '---------------CFL -----------------------
                If pVal.EventType = SAPbouiCOM.BoEventTypes.et_CHOOSE_FROM_LIST Then
                    Dim oCFLEvento As SAPbouiCOM.IChooseFromListEvent
                    oCFLEvento = pVal
                    Dim sCFL_ID As String
                    sCFL_ID = oCFLEvento.ChooseFromListUID
                    oForm = SBO_Application.Forms.Item(FormUID)
                    Dim oCFL As SAPbouiCOM.ChooseFromList
                    oCFL = oForm.ChooseFromLists.Item(sCFL_ID)
                    Try
                        If oCFLEvento.BeforeAction = False Then
                            Dim oDataTable As SAPbouiCOM.DataTable
                            oDataTable = oCFLEvento.SelectedObjects
                            If pVal.ItemUID = "SIJ27" Then
                                Try
                                    oEdit = oForm.Items.Item("24").Specific
                                    oEdit.String = oDataTable.GetValue("ItemName", 0)
                                    oEdit = oForm.Items.Item("SIJ27").Specific
                                    oEdit.String = oDataTable.GetValue("ItemCode", 0)
                                Catch ex As Exception
                                End Try
                            End If
                            If pVal.ItemUID = "29" And pVal.ColUID = "V_15" Then
                                oEdit = oMatrix.Columns.Item("V_15").Cells.Item(pVal.Row).Specific
                                oEdit.String = oDataTable.GetValue("CardCode", 0)
                            End If
                          
                        End If
                    Catch ex As Exception
                        ' SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Error)
                    End Try
                End If
                If pVal.FormMode = SAPbouiCOM.BoFormMode.fm_UPDATE_MODE Then
                    oItem = oForm.Items.Item("153")
                    oItem.Enabled = False
                ElseIf pVal.FormMode = SAPbouiCOM.BoFormMode.fm_OK_MODE Then
                    oItem = oForm.Items.Item("153")
                    oItem.Enabled = True
                End If
                '-----------End-----------------
            End If
        Catch ex As Exception
            If ex.Message <> "Form - Invalid Form" Then
                SBO_Application.StatusBar.SetText(ex.Message, SAPbouiCOM.BoMessageTime.bmt_Short, SAPbouiCOM.BoStatusBarMessageType.smt_Warning)
            End If
        End Try
    End Sub
    Private Sub SBO_Application_MenuEvent(ByRef pVal As SAPbouiCOM.MenuEvent, ByRef BubbleEvent As Boolean) Handles SBO_Application.MenuEvent
        Try
            If (pVal.MenuUID = "43530" Or pVal.MenuUID = "1288" Or pVal.MenuUID = "1289" Or pVal.MenuUID = "1290" Or pVal.MenuUID = "1291") And pVal.InnerEvent = False And pVal.BeforeAction = False Then
                oForm = SBO_Application.Forms.ActiveForm
                If oForm.UniqueID = "SEAI_JOB" Then
                    'If pVal.FormMode = SAPbouiCOM.BoFormMode.fm_OK_MODE Then
                    oItem = oForm.Items.Item("153")
                    oItem.Enabled = True
                    'End If
                End If
            End If
            If pVal.MenuUID = "1282" And pVal.InnerEvent = False And pVal.BeforeAction = False Then
                oForm = SBO_Application.Forms.ActiveForm
                If oForm.UniqueID = "SEAI_JOB" Then
                    oItem = oForm.Items.Item("153")
                    oItem.Enabled = False
                    DocNumber_SI()
                    oEdit = oForm.Items.Item("SIJ18").Specific
                    oEdit.String = Format(Now.Date, "dd/MM/yy")
                    ShippigNameLoad()
                    oMatrix3 = oForm.Items.Item("SIJGR").Specific
                    oMatrix3.AddRow()
                    oMatrix = oForm.Items.Item("SIJDOMAT").Specific
                    oMatrix.AddRow()
                    oMatrix1 = oForm.Items.Item("148").Specific
                    oMatrix1.AddRow()
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub
End Class
